package com.yuyou.fn.controller;

import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.settlement.entity.ChangeRecord;
import com.yuyou.fn.settlement.entity.Fee;
import com.yuyou.fn.settlement.entity.MainAccount;
import com.yuyou.fn.settlement.entity.Withdrawal;
import com.yuyou.fn.settlement.service.IFeeService;
import com.yuyou.fn.settlement.service.IMainAccountService;
import com.yuyou.fn.settlement.service.IWithdrawalService;
import com.yuyou.fn.settlement.vo.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by wens on 2017/3/13.
 */

@Controller
@RequestMapping("/api/admin/settlement")
public class AdminSettlementController extends AdminApiBaseController {

    @Resource
    private IMainAccountService mainAccountService ;



    @Resource
    private IFeeService feeService ;

    @Resource
    private IWithdrawalService withdrawalService ;


    @ApiOperation("主体账户列表")
    @RequestMapping(value = "/mainAccountList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<MainAccount>> mainAccountList(HttpServletRequest request ) {
        SysUser user = getUser(request);
        List<MainAccount> mainAccountList = mainAccountService.findMainAccount(user.getAreaTeamId(), user.getBusTeamId());
        return ReturnResult.success(mainAccountList);
    }

    @ApiOperation("所有主体账户列表")
    @RequestMapping(value = "/allMainAccountList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<List<MainAccount>> allMainAccountList() {
        List<MainAccount> mainAccountList = mainAccountService.findMainAccount(null, null);
        return ReturnResult.success(mainAccountList);
    }


    @ApiOperation("账户明细报表")
    @RequestMapping(value = "/accountReportDetailList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<ChangeRecord>> accountReportDetailList(AccountReportQueryVo accountReportQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        if(accountReportQueryVo.getEndDate()!= null ){
            DateUtils.addDays(accountReportQueryVo.getEndDate() , 24 );
        }

        Page<ChangeRecord> page = mainAccountService.findAccountReportDetailList( accountReportQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }


    @ApiOperation("费用列表")
    @RequestMapping(value = "/feelList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Fee>> feelList(FeeQueryVo feeQueryVo , @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        if(feeQueryVo.getEndDate() != null ){
            DateUtils.addDays(feeQueryVo.getEndDate() , 24 );
        }
        Page<Fee> page = feeService.findFeeByQuery(feeQueryVo, pageNo, pageSize);
        return ReturnResult.success(page);
    }

    @ApiOperation("保存费用")
    @RequestMapping(value = "/saveFee" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Fee>> saveFee(FeeSaveVo feeSaveVo) {
        feeService.saveFee( feeSaveVo );
        return ReturnResult.success();
    }

    @ApiOperation("审核费用")
    @RequestMapping(value = "/auditingFee" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Fee>> auditingFee(@RequestParam("feeId") Long feeId ,@RequestParam("status") Integer status) {
        feeService.auditingFee( feeId , status );
        return ReturnResult.success();
    }


    @ApiOperation("利润明细")
    @RequestMapping(value = "/profitDetail" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<ProfitDetailVo>> profitDetail(@RequestParam("mainAccountId") Long mainAccountId , @RequestParam("startDate") Date startDate , @RequestParam("endDate") Date endDate , HttpServletRequest request ) {
        if(endDate != null ){
            DateUtils.addDays(endDate , 24 );
        }
        ProfitDetailVo profitDetailVo = mainAccountService.calProfit(mainAccountId, startDate, endDate);
        return ReturnResult.success(profitDetailVo);
    }


    @ApiOperation("账户提现列表")
    @RequestMapping(value = "/accountWithdrawalList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Withdrawal>> accountWithdrawalList(@RequestParam("mainAccountId") Long mainAccountId, @RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        WithdrawalQueryVo withdrawalQueryVo = new WithdrawalQueryVo();
        withdrawalQueryVo.setMainAccountId(mainAccountId);
        Page<Withdrawal> page  = withdrawalService.findWithdrawalByQuery(withdrawalQueryVo , pageNo,pageSize) ;
        return ReturnResult.success(page);
    }

    @ApiOperation("提现列表")
    @RequestMapping(value = "/withdrawalList" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Page<Withdrawal>> withdrawalList(WithdrawalQueryVo withdrawalQueryVo ,@RequestParam(value = "pageNo" ,defaultValue = "1") int pageNo, @RequestParam(value = "pageSize" ,defaultValue = "20") int pageSize, HttpServletRequest request ) {
        SysUser user = getUser(request);
        withdrawalQueryVo.setAreaTeamId(user.getAreaTeamId());
        Page<Withdrawal> page  = withdrawalService.findWithdrawalByQuery(withdrawalQueryVo , pageNo,pageSize) ;
        return ReturnResult.success(page);
    }

    @ApiOperation("提现申请")
    @RequestMapping(value = "/applyWithdrawal" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> applyWithdrawal(@RequestParam("mainAccountId") Long mainAccountId,@RequestParam("amount") BigDecimal amount ,@RequestParam("bankName") String bankName ,@RequestParam("bankCity") String bankCity ,@RequestParam("cardUser") String cardUser, @RequestParam("cardNo") String cardNo, HttpServletRequest request ) {
        withdrawalService.applyWithdrawal(mainAccountId,amount,bankName,bankCity,cardUser,cardNo);
        return ReturnResult.success("OK");
    }

    @ApiOperation("更改提现状态")
    @RequestMapping(value = "/updateWithdrawalStatus" ,method = { RequestMethod.GET ,RequestMethod.POST })
    @ResponseBody
    public ReturnResult<Void> updateWithdrawalStatus(@RequestParam("withdrawalId") Long withdrawalId ,@RequestParam("status")Integer status, HttpServletRequest request ) {
        withdrawalService.updateWithdrawalStatus(withdrawalId,status);
        return ReturnResult.success("OK");
    }


}
